import Vue from "vue"
import Vuex from "vuex"

import axios from "axios"
Vue.use(Vuex)   // 全局声明 使用 vuex
// 每一个 Vuex 应有的核心就是store（仓库）
// 1.Vuex 的状态存储是响应式的
// 2.不能直接改变 store中的状态 改变store中的状态的唯一路径就是显式的提交（commit） mutation
// 全局的状态
const store = new Vuex.Store({
    state: {
        count: 1000,
        city: "湖北孝感",
        msg: "wuhan2110",
        word: "Are you OK",
        data: {
            test: "wh2110 可劲欠债",
            text: "说好得自信呢"
        },
        show: true
    },
    // 唯一修改数据的地方
    mutations: {
        // 更Vuex 的 store 中的状态的唯一方法是提交 mutation
        // 只能进行同步操作
        // mutationsType(state) {

        // },
        changeMessage(state, payload) {
            state.msg = payload
        },
        countAdd(state) {
            state.count++
        },
        countDesc(state) {
            state.count--
        },
        changeCount(state, payload) {
            state.count += payload
        },
        changeCity(state, payload) {
            state.city = payload
        },
        changeMsg(state, payload) {
            state.msg = payload
        },
        changeWord(state, payload) {
            state.word = payload
        },
        changeDataTest(state, payload) {
            state.data = { ...state.data, test: payload }
        },
        changeShow(state, payload) {
            state.show = payload
        },
        changeDataText(state, payload) {
            state.data = { ...state.data, text: payload }
        },
    },
    // 动作信号
    actions: {
        // Action 包含异步操作
        // Action 必须提交 commit  mutation
        changeMessageAction(context, payload) { // context={commit:function(){}}
            context.commit("changeMessage", payload)
            // commit("changeMessage")

        },
        changeDataTextAction({ commit }, payload) {
            commit("changeDataText", payload);
        },

        changeDataTextActionAsync({ commit }) {
            axios.get("http://localhost:2110/test")
                .then(res => {
                    console.log(res);
                    commit("changeDataText", res.data[0]);

                })

        }
    },
    // 依赖state 返回新的状态
    getters: {
        newCount(state) {
            return state.count * 10 + 8888
        }
    }

})

export default store